package controllers.admin;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import models.Administrator;
import models.Article;
import models.Brand;
import models.Message;
import models.QueryBean;
import models.Slide;
import play.data.validation.Valid;
import play.mvc.Controller;
import play.mvc.With;
import controllers.GlobalVariables;
import controllers.Secure;
import controllers.ValidationUtil;

/**
 * @author ray.wu
 *
 */
@With(Secure.class)
public class Articles extends Controller {
	/**
	 * 进入文章管理页面
	 */
	public static void index() {
		QueryBean queryBean = new QueryBean();
		render(queryBean);
	}
	
	public static void indexTable(QueryBean queryBean) {
		if(queryBean == null) {
			queryBean = new QueryBean();
			queryBean.isDesc = true;
		}
		StringBuilder hql = new StringBuilder("id != null ");
    	List<Object> objs = new ArrayList<Object>();
    	
    	if(!ValidationUtil.isEmpty(queryBean.keyword)) {
    		if(ValidationUtil.isLong(queryBean.keyword)) {
    			hql.append("and (id=? or");
        		objs.add(Long.valueOf(queryBean.keyword));
    		} else {
    			hql.append("and (");
    		}
    		hql.append(" title like ?) ");
    		objs.add("%"+queryBean.keyword+"%");
    	}
    	
    	if(!ValidationUtil.isEmpty(queryBean.orderby)) {
    		hql.append(" order by " + queryBean.orderby);
    		if(queryBean.isDesc) 
    			hql.append(" desc ");
    		else
    			hql.append(" asc ");
    	} else {
    		hql.append(" order by createAt desc, lastModifiedAt desc ");
    	}
		
		//页数计算
		queryBean.totalQuantity = Article.count(hql.toString(), objs.toArray());
		queryBean.totalPage = (queryBean.totalQuantity/queryBean.quantityEachPage + 1);
		if(queryBean.totalPage < queryBean.currentPage) {
			queryBean.currentPage = queryBean.totalPage;
		}
		
		List<Article> articles = Article.find(hql.toString(), objs.toArray()).fetch(
				(int)queryBean.currentPage, queryBean.quantityEachPage);
		
		render(articles, queryBean);
	}
	
	/**
	 * 新建文章页面。
	 */
	public static void newArticle() {
		Article article = new Article();
		render(article);
	}
	
	/**
	 * 进入文章修改页面
	 */
	public static void editArticle(long id){
		Article article = Article.findById(id);
		render(article);
	}
	
	/**
	 * 删除文章
	 * @param articleIdVal
	 * @return
	 */
	public static String deleteArticle(long articleIdVal){
		List<Slide> slList = Slide.find("from Slide slide where slide.article.id="+articleIdVal, new Object[]{}).fetch();
		if(slList!=null && slList.size()>0){
			for(Slide sl : slList){
				sl.article = null;
				sl.delete();
			}
		}
		
		Article ar = Article.findById(articleIdVal);
		ar.createByAdmin = null;
		ar.lastModifiedByAdmin = null;
		ar.save();
		ar.delete();
		
		return "SUCCESS";
	}
	
	/**
	 * 创建文章
	 * 
	 * @param articleTitleVal
	 * @param articleContentVal
	 * @return
	 */
	public static void createArticle(@Valid Article article){
		if(validation.hasErrors()) {
//			validation.keep();
			render("@newArticle", article);
		}
		String username = session.get(GlobalVariables.SECURE_ADMINNAME);
		Administrator admin = Administrator.find("byUsername", username).first();
		
		article.setCreator(admin);
		article.resetModifier(admin);
		article.save();
		index();
	}
	
	/**
	 * 修改文章
	 * 
	 * @param articleTitleVal
	 * @param articleContentVal
	 * @return
	 */
	public static void updateArticle(@Valid Article article){
		if(validation.hasErrors()) {
			render("@editArticle", article);
		}
		String username = session.get(GlobalVariables.SECURE_ADMINNAME);
		Administrator admin = Administrator.find("byUsername", username).first();
		
		Article ar = Article.findById(article.id);
		ar.title = article.title;
		ar.content = article.content;
		ar.resetModifier(admin);
		
		ar.save();
		
		index();
	}
	
	/**
	 * 异步加载文章内容
	 * 
	 * @param articleIdVal
	 * @return
	 */
	public static String getArticle(long articleIdVal){
		Article ar = Article.findById(articleIdVal);
		StringBuilder result = new StringBuilder();
		result.append("articleTitleVal_update:"+ar.title);
		result.append("#");
		result.append("articleContentVal_update:"+ar.content);
		return result.toString();
	}
	
}